package com.company.gobang.module.hall.controller;

import com.company.gobang.module.hall.service.HallService;
import com.company.gobang.proto.MsgType;
import com.company.gobang.proto.RequestGameReq;
import com.company.gobang.proto.UserChallengeReq;
import com.company.gobang.proto.UserCreateReq;
import xyz.noark.core.annotation.Autowired;
import xyz.noark.core.annotation.Controller;
import xyz.noark.core.annotation.PlayerId;
import xyz.noark.core.annotation.controller.CommandMapping;
import xyz.noark.core.annotation.controller.ExecThreadGroup;
import xyz.noark.core.network.NetworkProtocol;
import xyz.noark.core.network.Session;
import xyz.noark.core.network.Session.State;
import xyz.noark.network.SocketSession;

/**
 * 大厅入口
 *
 * @author 小流氓[176543888@qq.com]
 */
@Controller(threadGroup = ExecThreadGroup.ModuleThreadGroup)
public class HallController {

    @Autowired
    private HallService hallService;

    /**
     * 创建用户请求
     * <p>
     * 这里算是Socket链接上来第一个请求，算登录吧，所以这个入口的state是State.CONNECTED
     */
    @CommandMapping(opcode = MsgType.UserCreate, state = State.CONNECTED)
    public NetworkProtocol createReq(SocketSession session, UserCreateReq req) {
        return hallService.createReq(session, req.getName());
    }

    /**
     * 获取用户列表请求
     * <p>
     * 1. 上面那个协议已算登录成功了，这里就不要状态了
     * 2. 这里可以直接使用 @PlayerId String playerId 注入这个已登录的玩家ID
     * 3. 获取用户列表请求并没有参数，所以这个协议对象也可以免了
     * </p>
     */
    @CommandMapping(opcode = MsgType.UserList)
    public void listReq(@PlayerId String playerId) {
        hallService.listReq(playerId);
    }

    /**
     * 发起跳请求.
     */
    @CommandMapping(opcode = MsgType.UserChallenge)
    public NetworkProtocol challengeReq(@PlayerId String playerId, Session session, UserChallengeReq req) {
        return hallService.challengeReq(playerId, session, req.getName());
    }

    /**
     * 是否同意请求.
     */
    @CommandMapping(opcode = MsgType.RequestGame)
    public void gameReq(@PlayerId String playerId, RequestGameReq req) {
        hallService.gameReq(playerId, req.isAgree(), req.getName(), req.getTarget());
    }

}